package com.vctgo.system.config;

import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.vctgo.system.domain.po.CsDbConfigPo;
import com.vctgo.system.mapper.CsDbConfigMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.cache.RedisCache;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** 动态数据源配置 */
@Slf4j
@Component
public class DynamicDataSourceConfig {

    @Resource
    private DynamicRoutingDataSource dynamicRoutingDataSource;

    @Autowired
    private CsDbConfigMapper csDbConfigMapper;

    @PostConstruct
    public void initAllDb() throws SQLException {
        List<CsDbConfigPo> dbConfigPos = csDbConfigMapper.selectList();
        for (CsDbConfigPo dbConfigPo : dbConfigPos) {
            DruidDataSource dataSource = getInitDBConfig();
            String url = "jdbc:mysql://"
                    +  dbConfigPo.getUrl() + "/" + dbConfigPo.getSchema()
                    + "?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
            dataSource.setUrl(url);
            dataSource.setUsername(dbConfigPo.getUsername());
            dataSource.setPassword(dbConfigPo.getPassword());
            dynamicRoutingDataSource.addDataSource(dbConfigPo.getTenantId().toString(), dataSource);
        }
    }

    public void addDBConfig(CsDbConfigPo dbConfigPo) throws SQLException {
        DruidDataSource dataSource = getInitDBConfig();
        String url = "jdbc:mysql://"
                +  dbConfigPo.getUrl() + "/" + dbConfigPo.getSchema()
                + "?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
        dataSource.setUrl(url);
        dataSource.setUsername(dbConfigPo.getUsername());
        dataSource.setPassword(dbConfigPo.getPassword());
        dynamicRoutingDataSource.addDataSource(dbConfigPo.getTenantId().toString(), dataSource);
    }

    public static DruidDataSource getInitDBConfig() throws SQLException {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setInitialSize(5);
        dataSource.setMinIdle(50);
        dataSource.setMaxActive(100);
        dataSource.setMaxWait(60000);
        dataSource.setTimeBetweenEvictionRunsMillis(60000);
        dataSource.setMinEvictableIdleTimeMillis(300000);
        dataSource.setValidationQuery("select 1 from dual");
        dataSource.setTestWhileIdle(true);
        dataSource.setTestOnBorrow(false);
        dataSource.setTestOnReturn(false);
        dataSource.setPoolPreparedStatements(true);
        dataSource.setMaxPoolPreparedStatementPerConnectionSize(40);
        dataSource.setFilters("stat,slf4j");
        dataSource.setConnectionProperties("druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000");
        return dataSource;
    }


}
